﻿@model IEnumerable<FreeWorkflow.DataModels.WorkflowModel>
    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }

    <h2>Index</h2>
<div style="float:right">
    @Html.ActionLink("New Workflow", "Create", null, new { @class = "btn btn-link" })
</div>
    
    <br />

    <table class="table">
        <tr>
            <th>Workflow</th>
            <th>Last Update</th>
            <th></th>
        </tr>
        @foreach (FreeWorkflow.DataModels.WorkflowModel item in Model)
        {
            <tr>
                <td>
                    @item.WorkflowName
                </td>
                <td>
                    @item.DateModified
                </td>
                <td>
                    <input type="button" name="NewInstance" value="New Instance" onclick="NewWorkflowInstance('/Workflow/InstantiateWorkflow?WorkflowTitle=@item.WorkflowName'); return;" />
                    <input type="button" name="Instances" value="Instances" onclick="ShowPopup('/Workflow/GetInstances?WorkflowTitle=@item.WorkflowName','Workflow Instances');return false;" />
                </td>
            </tr>
        }
    </table>

<script>
    function NewWorkflowInstance(url) {
        $.ajax({
            type: 'POST',
            cache: false,
            url: url,
            success: function(data) {
                if(data=="done")
                {
                    alert("Workflow Instance Created.");
                    location.reload();
                }
                else
                {
                    alert(data);
                }
            }
        });
    }
    function ShowPopup(url,Title) {
        //select Row
        var tag = $("<div></div>");
        $.ajax({
            url: url,
            cache: false,
            success: function (data) {

                tag.html(data);

                tag.dialog({ modal: true, width: 750, height: 450, title: Title});
                
                current_dialog = tag.dialog('open');
            }
        });
    }
    var current_dialog;
</script>

@section Scripts {
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/jqueryui")
}